import {
    createStore
} from 'vuex'
import {
    reactive
} from '@vue/reactivity'

export default createStore({
    state: {
        question: reactive([
            // {
            //     totalTitle: '问卷标题',
            //     totalRemark: '为了给您提供更好的服务，希望您能抽出几分钟时间，将您的感受和建议告诉我们，我们非常重视每位用户的宝贵意见，期待您的参与！现在我们就马上开始吧！',
            //     id: '1',
            //     inRecycleBin: false,
            //     questions: [{
            //     type: 'radio',
            //     title: '',
            //     remark: '',
            //     text: '',
            //     options:[
            //         {
            //             isEmpty: false,
            //             message: '选项'
            //         },
            //         {
            //             isEmpty: false,
            //             message: '选项'
            //         }
            //     ],
            //     required: false,
            //     isEdit: true,
            //     questionBoxShake: false
            // }]
            // }
        ]),
        answers: reactive([]),
        userName: reactive(''),
        count: -1,
        getAnswers:reactive([])
    },
    mutations: {
        changeUserName(state, userName){
            state.userName = userName
        },
        deleteOption(state, index) {
            state.question[index.id].questions[index.questionsIndex].options.splice(index.optionsIndex, 1)
        },
        addOption(state, message) {
            state.question[message.id].questions[message.index].options.push({
                message: '选项'
            })
        },
        updateOption(state, message) {
            state.question[message.id].questions[message.index].options[message.optionsIndex].message = message.value
        },
        addQuestion(state, id) {
            function guid() {
                return Number(Math.random().toString().substr(3, 3) + Date.now()).toString(36);
            }
            var qid = guid()
            state.question[id].questions.push({
                type: 'radio',
                title: '',
                remark: '',
                text: '',
                qid: qid,
                options: [{
                        isEmpty: '',
                        message: '选项'
                    },
                    {
                        isEmpty: '',
                        message: '选项'
                    }
                ],
                isEdit: true,
                required: '',
                questionBoxShake: ''
            })
        },
        deleteQuestion(state, index) {
            state.question[index.id].questions.splice(index.index, 1)
        },
        updateTotalTitle(state, message) {
            state.question[message.id].totalTitle = message.value
        },
        updateTotalRemark(state, message) {
            state.question[message.id].totalRemark = message.value
        },
        updateTopic(state, message) {
            state.question[message.id].questions[message.index].type = message.value
        },
        updateText(state, message) {
            state.question[message.id].questions[message.index].text = message.value
        },
        updateQuestionTitle(state, message) {
            state.question[message.id].questions[message.index].title = message.value
        },
        updateQuestionRemark(state, message) {
            state.question[message.id].questions[message.index].remark = message.value
        },
        updateIsRequired(state, isRequired) {
            state.question[isRequired.id].questions[isRequired.index].required = isRequired.value
        },
        intoRecycleBin(state, index) {
            state.question[index].inRecycleBin = true
        },
        intoMineQuestion(state, index) {
            state.question[index].inRecycleBin = ''
        },
        deleteQuestionnaire(state, index) {
            state.question.splice(index, 1)
        },
        addQuestionnaire(state) {
            function guid() {
                return Number(Math.random().toString().substr(3, 3) + Date.now()).toString(36);
            }
            var id = guid();
            var qid = guid()
            state.question.push({
                totalTitle: '问卷标题',
                totalRemark: '为了给您提供更好的服务，希望您能抽出几分钟时间，将您的感受和建议告诉我们，我们非常重视每位用户的宝贵意见，期待您的参与！现在我们就马上开始吧！',
                id: id,
                inRecycleBin: '',
                issue: '',
                questions: [{
                    type: 'radio',
                    title: '',
                    remark: '',
                    text: '',
                    qid: qid,
                    options: [{
                            isEmpty: '',
                            message: '选项'
                        },
                        {
                            isEmpty: '',
                            message: '选项'
                        }
                    ],
                    required: '',
                    isEdit: true,
                    questionBoxShake: ''
                }]
            })
        },
        getQuestion(state, response) {
            state.question = []
            state.answers = []
            for (let i = 0; i < response.message[0].qlist.length; i++) {
                state.question.push(
                    response.message[0].qlist[i]
                )
                for(let a = 0; a < response.message[0].qlist[i].questions.length; a++){
                    state.answers.push(
                        {
                            qid: state.question[i].questions[a].qid,
                            answer: []
                        }
                    )
                }
            }
            console.log(state.question)
        },
        changeAnswers(state, index){
            console.log(state.question)
            state.answers = []
            for(let i = 0; i < state.question[index].questions.length; i++){
                state.answers.push({qid: state.question[index].questions[i].qid, answer: []})
            }
            console.log(state.answers)
        },
        sureIssue(state, index){
            state.question[index].issue = true
        }
    },
})